<?php

namespace app\common\model;

use think\Model;

class ShareRecord extends Model
{
    // 表名
    protected $name = 'share_record';
    
    // 自动写入时间戳字段
    protected $autoWriteTimestamp = true;

    // 追加属性
    protected $append = [
        'create_time_text',
        'update_time_text',
	    'type_text',
	    'gift_type_text'
    ];
    

    CONST TYPE_WEB = 1;
	CONST TYPE_CODE = 2;
	CONST TYPE_GOODS = 3;
	CONST TYPE_SHOP = 4;
	
	public static $typeList = [
		self::TYPE_WEB => '分享商城',
		self::TYPE_CODE => '分享二维码',
		self::TYPE_GOODS => '分享商品',
		self::TYPE_SHOP => '分享店铺'
	];

	public function getTypeTextAttr($value, $data) {
		$value = $value ? $value : $data['type'];
		$list = self::$typeList;
		return isset($list[$value]) ? $list[$value] : '';
	}
	
	public function getGiftTypeTextAttr($value, $data) {
		$value = $value ? $value : $data['gift_type'];
		$list = FundsRecord::$_account_type;
		return isset($list[$value]) ? $list[$value] : '';
	}

    public function getCreateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['create_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }


    public function getUpdateTimeTextAttr($value, $data)
    {
        $value = $value ? $value : $data['update_time'];
        return is_numeric($value) ? date("Y-m-d H:i:s", $value) : $value;
    }

    protected function setCreateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

    protected function setUpdateTimeAttr($value)
    {
        return $value && !is_numeric($value) ? strtotime($value) : $value;
    }

	public function user() {
		return $this->belongsTo('user', 'user_id', 'id', '', 'left')
			->setEagerlyType(0);
	}
}
